home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / lib-tk / tkSimpleDialog.py < prev   
Encoding:
Python Source  |  2000-06-23  |  5.0 KB  |  220 lines

  1. #
  2. # An Introduction to Tkinter
  3. # tkSimpleDialog.py
  4. #
  5. # Copyright (c) 1997 by Fredrik Lundh
  6. #
  7. # fredrik@pythonware.com
  8. # http://www.pythonware.com
  9. #
  10.  
  11. # --------------------------------------------------------------------
  12. # dialog base class
  13.  
  14. from Tkinter import *
  15. import os
  16.  
  17. class Dialog(Toplevel):
  18.  
  19.     def __init__(self, parent, title = None):
  20.  
  21.         Toplevel.__init__(self, parent)
  22.         self.transient(parent)
  23.  
  24.         if title:
  25.             self.title(title)
  26.  
  27.         self.parent = parent
  28.  
  29.         self.result = None
  30.  
  31.         body = Frame(self)
  32.         self.initial_focus = self.body(body)
  33.         body.pack(padx=5, pady=5)
  34.  
  35.         self.buttonbox()
  36.  
  37.         self.grab_set()
  38.  
  39.         if not self.initial_focus:
  40.             self.initial_focus = self
  41.  
  42.         self.protocol("WM_DELETE_WINDOW", self.cancel)
  43.  
  44.         self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
  45.                                   parent.winfo_rooty()+50))
  46.  
  47.         self.initial_focus.focus_set()
  48.  
  49.         self.wait_window(self)
  50.  
  51.     #
  52.     # construction hooks
  53.  
  54.     def body(self, master):
  55.         # create dialog body.  return widget that should have
  56.         # initial focus.  this method should be overridden
  57.  
  58.         pass
  59.  
  60.     def buttonbox(self):
  61.         # add standard button box. override if you don't want the
  62.         # standard buttons
  63.         
  64.         box = Frame(self)
  65.  
  66.         w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
  67.         w.pack(side=LEFT, padx=5, pady=5)
  68.         w = Button(box, text="Cancel", width=10, command=self.cancel)
  69.         w.pack(side=LEFT, padx=5, pady=5)
  70.  
  71.         self.bind("<Return>", self.ok)
  72.         self.bind("<Escape>", self.cancel)
  73.  
  74.         box.pack()
  75.  
  76.     #
  77.     # standard button semantics
  78.  
  79.     def ok(self, event=None):
  80.  
  81.         if not self.validate():
  82.             self.initial_focus.focus_set() # put focus back
  83.             return
  84.  
  85.         self.withdraw()
  86.         self.update_idletasks()
  87.  
  88.         self.apply()
  89.  
  90.         self.cancel()
  91.  
  92.     def cancel(self, event=None):
  93.  
  94.         # put focus back to the parent window
  95.         self.parent.focus_set()
  96.         self.destroy()
  97.  
  98.     #
  99.     # command hooks
  100.  
  101.     def validate(self):
  102.  
  103.         return 1 # override
  104.  
  105.     def apply(self):
  106.  
  107.         pass # override
  108.  
  109.  
  110. # --------------------------------------------------------------------
  111. # convenience dialogues
  112.  
  113. import string
  114.  
  115. class _QueryDialog(Dialog):
  116.  
  117.     def __init__(self, title, prompt,
  118.                  initialvalue=None,
  119.                  minvalue = None, maxvalue = None,
  120.                  parent = None):
  121.  
  122.         if not parent:
  123.             import Tkinter
  124.             parent = Tkinter._default_root
  125.  
  126.         self.prompt   = prompt
  127.         self.minvalue = minvalue
  128.         self.maxvalue = maxvalue
  129.  
  130.         self.initialvalue = initialvalue
  131.  
  132.         Dialog.__init__(self, parent, title)
  133.  
  134.     def body(self, master):
  135.  
  136.         w = Label(master, text=self.prompt, justify=LEFT)
  137.         w.grid(row=0, padx=5, sticky=W)
  138.  
  139.         self.entry = Entry(master, name="entry")
  140.         self.entry.grid(row=1, padx=5, sticky=W+E)
  141.  
  142.         if self.initialvalue:
  143.             self.entry.insert(0, self.initialvalue)
  144.             self.entry.select_range(0, END)
  145.  
  146.         return self.entry
  147.  
  148.     def validate(self):
  149.  
  150.         import tkMessageBox
  151.  
  152.         try:
  153.             result = self.getresult()
  154.         except ValueError:
  155.             tkMessageBox.showwarning(
  156.                 "Illegal value",
  157.                 self.errormessage + "\nPlease try again",
  158.                 parent = self
  159.             )
  160.             return 0
  161.  
  162.         if self.minvalue is not None and result < self.minvalue:
  163.             tkMessageBox.showwarning(
  164.                 "Too small",
  165.                 "The allowed minimum value is %s. "
  166.                 "Please try again." % self.minvalue,
  167.                 parent = self
  168.             )
  169.             return 0
  170.  
  171.         if self.maxvalue is not None and result > self.maxvalue:
  172.             tkMessageBox.showwarning(
  173.                 "Too large",
  174.                 "The allowed maximum value is %s. "
  175.                 "Please try again." % self.maxvalue,
  176.                 parent = self
  177.             )
  178.             return 0
  179.                 
  180.         self.result = result
  181.  
  182.         return 1
  183.  
  184.  
  185. class _QueryInteger(_QueryDialog):
  186.     errormessage = "Not an integer."
  187.     def getresult(self):
  188.         return string.atoi(self.entry.get())
  189.  
  190. def askinteger(title, prompt, **kw):
  191.     d = apply(_QueryInteger, (title, prompt), kw)
  192.     return d.result
  193.  
  194. class _QueryFloat(_QueryDialog):
  195.     errormessage = "Not a floating point value."
  196.     def getresult(self):
  197.         return string.atof(self.entry.get())
  198.  
  199. def askfloat(title, prompt, **kw):
  200.     d = apply(_QueryFloat, (title, prompt), kw)
  201.     return d.result
  202.  
  203. class _QueryString(_QueryDialog):
  204.     def getresult(self):
  205.         return self.entry.get()
  206.  
  207. def askstring(title, prompt, **kw):
  208.     d = apply(_QueryString, (title, prompt), kw)
  209.     return d.result
  210.  
  211. if __name__ == "__main__": 
  212.  
  213.     root = Tk()
  214.     root.update()
  215.  
  216.     print askinteger("Spam", "Egg count", initialvalue=12*12)
  217.     print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100)
  218.     print askstring("Spam", "Egg label")
  219.  
  220.